// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections;
using System.Linq;

namespace SnapObjects.Data
{
    internal class PropertiesStateTracker : IPropertiesStateTracker
    {
        private readonly BitArray _items;

        public PropertiesStateTracker(int propertyCount)
        {
            this.PropertyCount = propertyCount;

            _items = new BitArray(propertyCount, false);
        }

        public int PropertyCount { get; }

        public bool IsModified(int index)
        {
            return _items[index];
        }

        public void MarkModified(int index)
        {
            _items[index] = true;
        }

        public void MarkNotModified(int index)
        {
            _items[index] = false;
        }

        public void MarkAllModified()
        {
            _items.SetAll(true);
        }

        public void MarkAllNotModified()
        {
            _items.SetAll(false);
        }

        public void MarkModified(int indexStart, int indexEnd)
        {
            for (var i = indexStart; i <= indexEnd; i++)
            {
                _items[i] = true;
            }
        }

        public void MarkNotModified(int indexStart, int indexEnd)
        {
            for (var i = indexStart; i <= indexEnd; i++)
            {
                _items[i] = false;
            }
        }

        public bool HasModified => _items.Cast<bool>().Contains(true);
    }
}
